<template>
  <div class="mod-faceFeature">
    <avue-crud
      ref="crudRef"
      :page="page"
      :data="dataList"
      :option="tableOption"
      :permission="permission"
      @search-change="onSearch"
      @on-load="getDataList"
    >
      <template #featureImageUrl="scope">
        <img
          v-if="scope.row.featureImageUrl"
          alt=""
          :src="scope.row.featureImageUrl"
          width="100"
          height="100"
        />
        <img
          v-else
          alt=""
          src="~@/assets/img/def.png"
          width="100"
          height="100"
        />
      </template>
      <template #featureType="scope">
        <el-tag :type="getFeatureTypeTag(scope.row.featureType)">
          {{ getFeatureTypeLabel(scope.row.featureType) }}
        </el-tag>
      </template>
      <template #status="scope">
        <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
          {{ scope.row.status === 1 ? "启用" : "禁用" }}
        </el-tag>
      </template>
      <template #visible="scope">
        <el-tag :type="scope.row.visible ? 'success' : 'info'">
          {{ scope.row.visible ? "可见" : "隐藏" }}
        </el-tag>
      </template>
      <template #menu-left>
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="addOrUpdateHandle()"
        >
          新增
        </el-button>
      </template>
      <template #menu="scope">
        <el-button
          type="primary"
          icon="el-icon-edit"
          @click="addOrUpdateHandle(scope.row)"
        >
          编辑
        </el-button>
        <el-button
          :type="scope.row.status === 1 ? 'warning' : 'success'"
          :icon="scope.row.status === 1 ? 'el-icon-close' : 'el-icon-check'"
          @click="statusChangeHandle(scope.row)"
        >
          {{ scope.row.status === 1 ? "禁用" : "启用" }}
        </el-button>
        <el-button
          type="danger"
          icon="el-icon-delete"
          @click="deleteHandle(scope.row.featureId)"
        >
          删除
        </el-button>
      </template>
    </avue-crud>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdateRef"
      @refreshDataList="getDataList"
    />
  </div>
</template>

<script setup>
import { ref, reactive, nextTick } from "vue";
import { isAuth } from "@/utils";
import { ElMessage, ElMessageBox } from "element-plus";
import AddOrUpdate from "./add-or-update.vue";
import { tableOption } from "@/crud/prod/faceFeature.js";
const resourcesUrl = import.meta.env.VITE_APP_RESOURCES_URL

const permission = ref({
  addBtn: isAuth("prod:faceFeature:save"),
  editBtn: isAuth("prod:faceFeature:update"),
  delBtn: isAuth("prod:faceFeature:delete"),
});

const dataList = ref([]);
const dataListLoading = ref(false);
const dataListSelections = ref([]);
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10, // 每页显示多少条
});

const addOrUpdateVisible = ref(false);
const addOrUpdateRef = ref(null);

/**
 * 获取数据列表
 */
const getDataList = (pageParam, params, done) => {
  dataListLoading.value = true;
  http({
    url: http.adornUrl("/admin/faceFeature/page"),
    method: "get",
    params: http.adornParams(
      Object.assign(
        {
          page: pageParam == null ? page.currentPage : pageParam.currentPage,
          limit: pageParam == null ? page.pageSize : pageParam.pageSize,
        },
        params
      )
    ),
  })
    .then(({ data }) => {
      dataList.value = data.records || data.list || [];
      data.records.forEach(item => {
        item.featureImageUrl = item.featureImageUrl ? resourcesUrl + item.featureImageUrl : ''
      })
      dataList.value = data.records


      page.total = data.total || 0;
      dataListLoading.value = false;
      if (done) done();
    })
    .catch(() => {
      dataListLoading.value = false;
      if (done) done();
    });
};

/**
 * 新增 / 修改
 * @param val
 */
const addOrUpdateHandle = (val) => {
  addOrUpdateVisible.value = true;
  console.log(val, addOrUpdateVisible.value, addOrUpdateRef.value);
  nextTick(() => {
    addOrUpdateRef.value?.init(val);
  });
};

/**
 * 删除
 * @param id
 */
const deleteHandle = (id) => {
  const ids = id
    ? [id]
    : dataListSelections.value?.map((item) => {
        return item.featureId;
      });
  ElMessageBox.confirm(`确定进行[${id ? "删除" : "批量删除"}]操作?`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      http({
        url: http.adornUrl("/admin/faceFeature"),
        method: "delete",
        data: http.adornData(ids, false),
      }).then(() => {
        ElMessage({
          message: "操作成功",
          type: "success",
          duration: 1500,
          onClose: () => {
            getDataList(page);
          },
        });
      });
    })
    .catch(() => {});
};

/**
 * 状态变更
 * @param row
 */
const statusChangeHandle = (row) => {
  const action = row.status === 1 ? "禁用" : "启用";
  ElMessageBox.confirm(`确定要${action}该五官资源?`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      http({
        url: http.adornUrl("/admin/faceFeature/status"),
        method: "put",
        data: http.adornData({
          featureId: row.featureId,
          status: row.status === 1 ? 0 : 1,
        }),
      }).then(() => {
        ElMessage({
          message: "操作成功",
          type: "success",
          duration: 1500,
          onClose: () => {
            getDataList(page);
          },
        });
      });
    })
    .catch(() => {});
};

/**
 * 获取五官类型标签
 */
const getFeatureTypeTag = (type) => {
  const tags = {
    1: "primary",
    2: "success",
    3: "warning",
    4: "info",
    5: "danger",
    6: "primary",
  };
  return tags[type] || "info";
};

/**
 * 获取五官类型标签文本
 */
const getFeatureTypeLabel = (type) => {
  const labels = {
    1: "左眼",
    2: "右眼",
    3: "左眉",
    4: "右眉",
    5: "左腮红",
    6: "右腮红",
    7: "嘴巴",
    8: "发型",
  };
  return labels[type] || "未知";
};

/**
 * 条件查询
 */
const onSearch = (params, done) => {
  getDataList(page, params, done);
};
</script>

<style scoped lang="scss">
.mod-faceFeature {
  :deep(.el-image) {
    border-radius: 4px;
    border: 1px solid #ebeef5;
  }
}
</style>
